from pymysql import Connection

class GB101:
    dwzh = None
    dwmc = None
    dwkhrq = None

    def __init__(self, dwzh, dwmc, dwkhrq):
        self.dwzh = dwzh
        self.dwmc = dwmc
        self.dwkhrq = dwkhrq

    def __str__(self):
        return f'dwzh:{self.dwzh}, dwmc:{self.dwmc}, dwkhrq:{self.dwkhrq}'

import random

def get_dwzh(count):
    dwzh_len = 9
    zero = dwzh_len - len(str(count))
    dwzh = "1"
    while zero >= 1:
        dwzh += '0'
        zero -= 1
    dwzh = dwzh + str(count)
    return dwzh

dwmc_prefix = '深圳市';
dwmc_list = ['快', '采', '网', '络', '星', '星', '英', '电', '营', '战', '古', '创', '众', '派', '协', '康', '三', '投', '猛']
dwmc_suffix = '有限公司'

def get_dwmc(count):
    dwmc_len = random.randint(3, 6)
    dwmc_len = count % dwmc_len
    dwmc_zhong = ''
    if dwmc_len == 0:
        dwmc_len = random.randint(3, 6)
    while dwmc_len > 0:
        dwmc_index = random.randint(1, len(dwmc_list)-1)
        dwmc_zhong += dwmc_list[dwmc_index]
        dwmc_len -= 1
    dwmc = dwmc_prefix + dwmc_zhong + dwmc_suffix
    return dwmc


dwkhrq_year = ['2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008', '2009']
dwkhrq_month = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']
dwkhrq_day = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']
def get_dwkhrq():
    year_index = random.randint(1, len(dwkhrq_year)-1)
    year = dwkhrq_year[year_index]
    month_index = random.randint(1, len(dwkhrq_month)-1)
    month = dwkhrq_month[month_index]
    day_index = random.randint(1, len(dwkhrq_day)-1)
    day = dwkhrq_day[day_index]
    return f'{year}-{month}-{day}'

def get_dwxx_count():
    conn = Connection(
        host='localhost',
        port=3306,
        user='root',
        password='admin'
    )
    conn.select_db('excel')
    cursor = conn.cursor()
    cursor.execute("select count(1) as count from dwxx")
    results: tuple = cursor.fetchall()
    return results[0][0]


def get_gb101_info(batch_size):
    gb101_list = []
    init_count = get_dwxx_count()
    count = init_count
    gb101_info = 'insert into DWXX (DWZH, DWMC, DWKHRQ, CREATEDUSER) VALUES '
    while count < batch_size + init_count:
        gb101_info += f"('{get_dwzh(count)}', '{get_dwmc(count)}', '{get_dwkhrq()}', 'BATCH'),"
        count = count + 1
        # print(count)

    gb101_info = gb101_info[0:len(gb101_info)-1:1]
    return gb101_info

def insert_dwxx(dwxx_insert_sql):
    conn = Connection(
        host='localhost',
        port=3306,
        user='root',
        password='admin',
        autocommit=True
    )
    conn.select_db('excel')
    cursor = conn.cursor()
    # cursor.execute("insert into DWXX (DWZH, DWMC, DWKHRQ, CREATEDUSER) VALUES ('1000000000', '深圳市有限公司', '2003-12-10', 'BATCH')")
    cursor.execute(dwxx_insert_sql)
    cursor.fetchall()
    print("insert end")

if '__main__' == __name__:
    # print(get_dwmc(10000))
    # print(get_dwkhrq())
    gb101_list = get_gb101_info(1000)
    print(gb101_list)
    insert_dwxx(gb101_list)



